/*
 * @Descripttion: react中的compose模块，用于将多个中间件组合成一个函数
 * @Author: lukasavage
 * @Date: 2021-11-28 11:48:04
 * @LastEditors: lukasavage
 * @LastEditTime: 2021-11-28 12:21:00
 */

function compose(...funcs) {
    if (funcs.length === 0) {
        return arg => arg
    }

    if (funcs.length === 1) {
        return funcs[0]
    }

    return funcs.reduce((a, b) => (...args) => a(b(...args)))
}

export default compose;

/*
debugger例子↓↓↓
假设两个函数要组合起来
function add1(str){
    return '1'+str;
}
function add2(str){
    return '2'+str;
}
------------------
// 第一次: a = add2 b = add1   => (...args)=> add2(add1(...args));
// compose返回的结果 (...args)=> add2(add1(...args));
// 
let fn = compose(add2, add1);    
let result = fn('originDispatch');
console.log(result);    ==> '12originDispatch'


*/